package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.mapper.*;
import com.srzk.youthleagueback.pojo.*;
import com.srzk.youthleagueback.results.Result;
import com.srzk.youthleagueback.utils.FileUtil;
import com.srzk.youthleagueback.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.srzk.youthleagueback.results.CodeMsg.*;
import static com.srzk.youthleagueback.results.CodeMsg.NOT_EXIST;

@Service
public class InnovationService {
    @Autowired
    private InnovationMapper mapper;
    @Autowired
    private InnovationPicMapper picMapper;

    public Result<Map<String,Long>> add(Innovation object, MultipartFile[] files) {
        int result = mapper.insert(object);
        if (result == 1) {
            List<String> url = new ArrayList<>();
           if(files!=null){
               try {
                   for(MultipartFile file:files){
                       String path =  FileUtil.handlePicture("part4",file);
                       InnovationPic pic = new InnovationPic();
                       pic.setPicture(path);
                       pic.setId(object.getId());
                       picMapper.insert(pic);
                   }
               }catch (Exception e){
                   return Result.error(File_FAIL);
               }
           }
            HashMap<String,Long> map = new HashMap<>();
            map.put("id",object.getId());
            return Result.success(map);
        } else {
            return Result.error(ADD_FAIL);
        }
    }

    public  Result<String> delete(String user, String year, Long id) {
        picMapper.deleteByUserAndYearAndTypeAndId(id);
        int result = mapper.deleteByUserAndYearAndTypeAndId(user, year, id);
        if(result==1){

            return Result.success("删除成功");
        }else{
            return Result.error(DELETE_FAIL);
        }
    }

    public  Result<String> update(Innovation object,MultipartFile[] files) {
        picMapper.deleteByUserAndYearAndTypeAndId(object.getId());
        int result = mapper.updateByPrimaryKey(object);
        if(result==1){

            List<String> url = new ArrayList<>();
            if(files!=null){
                try {
                    for(MultipartFile file:files){
                        String path =  FileUtil.handlePicture("part4",file);
                        InnovationPic pic = new InnovationPic();
                        pic.setPicture(path);
                        pic.setId(object.getId());
                        picMapper.insert(pic);
                    }
                }catch (Exception e){
                    return Result.error(File_FAIL);
                }
            }
            return Result.success("更新成功");
        }else{
            return Result.error(UPDATE_FAIL);
        }
    }

    public Result<Map<String,Object>> getList(String user, String year) {
        List<Innovation> object = mapper.selectByPrimaryKeyList(user, year);
        List<Map<String,Object>> resultList = new ArrayList<>();
        Map<String,Object> result = new HashMap<>();
        if(object!=null){
            object.forEach(item ->{

                List<String> url = new ArrayList<>();
                List<InnovationPic> pics = picMapper.selectByPrimaryKeyList(item.getId());
                for(InnovationPic pic:pics){
                    url.add(pic.getPicture());
                }

                Map<String,Object> info = new HashMap<String, Object>();
                info.put("date",item.getDate());
                info.put("address",item.getAddress());
                info.put("theme",item.getTheme());
                info.put("form",item.getForm());
                info.put("content",item.getContent());
                info.put("result",item.getResult());

                Map<String,Object> objectMap = new HashMap<>();
                objectMap.put("innovationInfo",info);
                objectMap.put("id",item.getId());
                objectMap.put("pictures",url);
                resultList.add(objectMap);
            });
            result.put("innovationList",resultList);

            return Result.success(result);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String,Object>> get(String user, String year,Long id) {
        Innovation object = mapper.selectByPrimaryKey(id,user, year);
        if(object!=null){
            List<String> url = new ArrayList<>();

            List<InnovationPic> pics = picMapper.selectByPrimaryKeyList(object.getId());
            for(InnovationPic pic:pics){
                url.add(pic.getPicture());
            }

            Map<String,Object> info = new HashMap<String, Object>();
            info.put("date",object.getDate());
            info.put("address",object.getAddress());
            info.put("theme",object.getTheme());
            info.put("form",object.getForm());
            info.put("content",object.getContent());
            info.put("result",object.getResult());

            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("innovationInfo",info);
            objectMap.put("pictures",url);

            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }
    public Result<Map<String,Object>> getGradeList(String grade, String year) {
        List<Innovation> allData = mapper.selectByGrade( year);
        if (allData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();

            allData.forEach(items -> {
                List<Map<String, Object>> list = new ArrayList<>();
                if (ObjectUtil.getGrade(items.getUser()).equals(grade)) {
                    String getClass = ObjectUtil.getClass(items.getUser());

                    Map<String,Object> listItem = new HashMap<>();
                    listItem.put("address",items.getAddress());
                    listItem.put("date",items.getDate());
                    listItem.put("form",items.getForm());
                    listItem.put("content",items.getContent());
                    listItem.put("class",ObjectUtil.getClass(items.getUser()));
                    listItem.put("list",listItem);

                    boolean flag = false;
                    for (Map<String, Object> stringObjectMap : data) {
                        if (getClass.equals(stringObjectMap.get("class"))) {
                            ArrayList tempList = (ArrayList) stringObjectMap.get("list");
                            tempList.add(listItem);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        Map<String, Object> dataItem = new HashMap<>();
                        dataItem.put("class", getClass);
                        list.add(listItem);
                        dataItem.put("list", list);
                        data.add(dataItem);
                    }
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String,Object>> getHistoryList(String user) {
        List<Innovation> allData = mapper.selectAll(user);
        if (allData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();
            allData.forEach(items -> {
                ArrayList<Map<String, Object>> list;

                Map<String,Object> listItem = new HashMap<>();
                listItem.put("content",items.getContent());
                listItem.put("address",items.getAddress());
                listItem.put("date",items.getDate());
                listItem.put("form",items.getForm());

                boolean flag = false;
                for (Map<String, Object> datum : data) {
                    if(items.getYear().equals(datum.get("year"))){
                        list = (ArrayList<Map<String, Object>>) datum.get("list");
                        list.add(listItem);
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    list = new ArrayList<>();
                    Map<String, Object> dataItem = new HashMap<>();
                    dataItem.put("year", items.getYear());
                    list.add(listItem);
                    dataItem.put("list", list);
                    data.add(dataItem);
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }
}
